#include "MovableManager.h"

MovableManager::MovableManager()
{
	_animationHandlerFactory= new AnimationHandlerFactory();
}

void MovableManager::addMovable(Movable* movable)
{
	_movables.push_back(movable);
}

void MovableManager::updateMovables()
{
	for(unsigned int i=0; i<_movables.size(); i++)
	{
		_movables[i]->updateAnimation();
	}
}

void MovableManager::moveMovables(float delta)
{
	for(unsigned int i=0; i<_movables.size(); i++)
	{
		_movables[i]->move(delta);
	}
}

hitResult MovableManager::resolveMovableCollision(Avatar* avatar)
{
	float pos_x=avatar->getShape()->getPosition().x;
	float pos_y=avatar->getShape()->getPosition().y;

	for(unsigned int i=0; i<_movables.size(); i++)
	{
		RectangleShape* shape=_movables[i]->getShape();
		if (abs(shape->getPosition().x -pos_x) <90 && abs(shape->getPosition().y -pos_y)<90) {
			if (shape->getGlobalBounds().intersects(avatar->getShape()->getGlobalBounds()))
		{
			if(avatar->getVelocity().y>50)
			{
				_movables.erase(_movables.begin()+i);
				return Success;
			}
			else
			{
				if(!avatar->isInvicible())
				{
					avatar->lose_heart();
					return Fail;
				}
			}
		}
		}
	}
	return None;
}